﻿using System;
using System.Web.UI;
using VipCo.Entities;
using VipCo.Data;

public partial class Admin_TinTuc_CNTinTuc : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpTinTuc + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
            BindCatalog.BindTinTuc(cboTinTuc);
            if (Request.QueryString["id"] != null)
            {

                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }



    protected void BindData(int IdChuyenMuc)
    {
        using (TinTuc entTinTuc = DataRepository.TinTucProvider.GetById(IdChuyenMuc))
        {
            txtTieuDeTin.Text = entTinTuc.TieuDe;
            chkTinNong.Checked = entTinTuc.TrangThai;
            cboTinTuc.SelectedValue = entTinTuc.IdNhomTin.ToString();
            // Bind du lieu
            BindCatalog.BindSubTinTuc(cboSubTintuc, Convert.ToInt32(cboTinTuc.SelectedValue));
            cboSubTintuc.SelectedValue = entTinTuc.IdSubTin.ToString();
            H_AnhDaiDien.Value = entTinTuc.AnhDaiDien;
            imgViewer.ImageUrl = entTinTuc.AnhDaiDien.Length > 0 ? Common.sPathUpTinTuc + entTinTuc.AnhDaiDien : "~/Images/no_image.jpg";
            txtTomTat.Text = entTinTuc.TomTat;
            txtContent.Text = Server.HtmlDecode(entTinTuc.NoiDung);
            txtTacGia.Text = entTinTuc.TacGia;
        }
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (TinTuc entTinTuc = new TinTuc()
                {
                    Id = Convert.ToInt32(H_CatalogId.Value),
                    IdNhomTin = Convert.ToInt32(cboTinTuc.SelectedValue),
                    IdSubTin = Convert.ToInt32(cboSubTintuc.SelectedValue),
                    TieuDe = Common.StandardString(txtTieuDeTin.Text),
                    TomTat = Common.StandardString(txtTomTat.Text),
                    TrangThai = chkTinNong.Checked,
                    NoiDung = Server.HtmlEncode(txtContent.Text),
                    TacGia = Common.GetKeyUserInfo("TenTruyCap"),
                    NgayCapNhat = Common.GetServerDateTime()
                })
            {
                imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpChuyenMuc + H_AnhDaiDien.Value : "no_image.jpg";
                if (H_AnhDaiDien.Value == "no_image.jpg")
                {
                    entTinTuc.AnhDaiDien = "no_image.jpg";
                }
                else
                {
                    entTinTuc.AnhDaiDien = H_AnhDaiDien.Value.Length > 0 ? H_AnhDaiDien.Value : "no_image.jpg";
                }


                if (entTinTuc.Id == 0)
                {
                    updateStatus = DataRepository.TinTucProvider.Insert(entTinTuc);
                }
                else
                {
                    entTinTuc.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.TinTucProvider.Update(entTinTuc);
                }

                if (!updateStatus)
                    WebMessage.Show("Cập nhật Tin Tức thất bại!");
                else
                {
                    WebMessage.Show("Cập nhật Tin Tức thành công!");
                    H_CatalogId.Value = entTinTuc.Id.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNTinTuc.aspx.cs", "cmdLuu_Click", ex.ToString());
            WebMessage.Show("Cập nhật Tin Tức thất bại!");
        }
    }


    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        txtTieuDeTin.Text = "";
        txtTomTat.Text = "";
        chkTinNong.Checked = true;
        txtContent.Text = "";
        txtTacGia.Text = "";
        cboTinTuc.SelectedValue = "0";
    }

    /// <summary>
    /// Xu ly su kien thay doi khi chon Nhom Tin Tuc
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cboTinTuc_TextChanged(object sender, EventArgs e)
    {
        if (!cboTinTuc.SelectedValue.Equals("0"))
        {
            BindCatalog.BindSubTinTuc(cboSubTintuc, Convert.ToInt32(cboTinTuc.SelectedValue));
        }
    }
}
